package models

import (
	"fmt"
	_ "github.com/astaxie/beego/orm"
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

type GoUser struct {
	Id int `orm:"pk;auto;column(user_id)"`
	Name string `orm:"size(100);column(user_name)"`
	Sex  int `orm:"size(2);column(gender)"`
	CreateDate time.Time `orm:"type(date);column(create_date)"`
	UpdateDate time.Time `orm:"type(date);column(update_date)"`
	Age int `orm:"size(10);column(age)"`
	Account string `orm:"size(100)column(account)"`
	Phone string `orm:"size(100)column(phone)"`
	CompanyName string `orm:"size(100)column(company_name)"`
	IndustryType string `orm:"size(100)column(industry_type)"`
	TechnologyStack string `orm:"size(100)column(technology_stack)"`
	Province string `orm:"size(100)column(province)"`
	Region string `orm:"size(100)column(region)"`
	City string `orm:"size(100)column(city)"`
	AccountIsOk int `orm:"size(2);column(account_is_ok)"`
	HeadImageUrl string `orm:"size(100)column(head_image_url)"`
	OpenId string `orm:"size(100)column(open_id)"`
}

func (u *GoUser) TableName()string  {
	return "t_user"
}

func AddUser(u GoUser) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("insert into t_user(name,gender,create_date,update_date,age,account,phone,company_name,industry_type,technology_stack,province,region,city,account_is_ok,head_image_url,open_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", u.Name, u.Sex, u.CreateDate,u.UpdateDate, u.Age,u.Account,u.Phone,u.CompanyName,u.IndustryType,u.TechnologyStack,u.Province,u.Region,u.City,u.AccountIsOk,u.HeadImageUrl,u.OpenId).Exec()
	if err!=nil{
		panic(err)
	}
	return true
}

func QueryUser(id int) GoUser  {
	user := GoUser{}
	newOrm := orm.NewOrm()
	newOrm.Raw("select * from t_user where user_id = ?",id).QueryRow(&user)
	return user
}

func QueryUserByAccountOrOpenId( u GoUser) GoUser  {
	paramKey:=u.Account
	if paramKey==""{
		paramKey = u.OpenId
	}
	user := GoUser{}
	newOrm := orm.NewOrm()
	newOrm.Raw("select * from t_user where open_id = ?",paramKey).QueryRow(&user)
	return user
}

func QueryUserList(ids []string)  []GoUser{
	var users [] GoUser
	newOrm := orm.NewOrm()
	var str string
	for range ids {
		str+=",?"
	}
	str = str[1:]
	_, err := newOrm.Raw("select * from t_user where user_id in ("+str+")",ids).QueryRows(&users)
	fmt.Println(err)
	return users
}